//
//  SEGColorManager.m
//  Commom
//
//  Created by 山刘 on 2018/10/30.
//  Copyright © 2018年 山刘. All rights reserved.
//
#import "SEGColorManager.h"
#import <UIColor+SEGTheme.h>

@implementation SEGColorManager

/// 从mainBundle读取颜色配置
/// @param key key
+ (UIColor *)readColorFromPlist:(NSString *)key {
    if (!key || key.length <= 0) {
        return nil;
    }
    NSDictionary *defaultColorDict = @{
        @"T"   : @"#ffc40d",
        @"TP"  : @"#ebb200",
        @"S1"  : @"#ff2e2e",
        @"S1P" : @"#eb1515",
        @"S2"  : @"#ffc40d",
        @"S2P" : @"#ebb200",
        @"S3"  : @"#2170b3",
        @"S3P" : @"#125c9b",
        @"S4"  : @"#57ba22",
        @"S4P" : @"#42a30e",
        @"1"   : @"#333333",
        @"2"   : @"#666666",
        @"3"   : @"#999999",
        @"C"   : @"#c0c0c0",
        @"L2"  : @"#e5e5e5",
        @"L3"  : @"#f5f5f5",
        @"L4"  : @"#e5e5e5",
        @"BG"  : @"#f8f8f8",
        @"W"   : @"#ffffff",
        @"W2"  : @"#ffffff",
        @"W3"  : @"#ffffff",
        @"TABLE": @"#ffffff",
    };
    // 默认模式
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"SEGColor" ofType:@"plist"];
    UIColor *anyColor;
    if (plistPath && plistPath.length > 0) {
        NSDictionary *colorDict = [NSDictionary dictionaryWithContentsOfFile:plistPath];
        if ([colorDict.allKeys containsObject:key]) {
            NSString *hexStr = colorDict[key];
            anyColor = [self colorWithHexString:hexStr];
        }
    }
    // 暗黑模式
    NSString *darkPlistPath = [[NSBundle mainBundle] pathForResource:@"SEGDarkColor" ofType:@"plist"];
    UIColor *darkColor;
    if (darkPlistPath && darkPlistPath.length > 0) {
        NSDictionary *darkColorDict = [NSDictionary dictionaryWithContentsOfFile:darkPlistPath];
        if ([darkColorDict.allKeys containsObject:key]) {
            NSString *hexStr = darkColorDict[key];
            darkColor = [self colorWithHexString:hexStr];
            // 特殊逻辑处理: 字体颜色透明度
            if ([key isEqualToString:@"2"]) {
                darkColor = [darkColor colorWithAlphaComponent:0.65];
            } else if ([key isEqualToString:@"3"]) {
                darkColor = [darkColor colorWithAlphaComponent:0.45];
            } else if ([key isEqualToString:@"C"]) {
                darkColor = [darkColor colorWithAlphaComponent:0.25];
            }
        }
    }
    if (anyColor && darkColor) {
        return [UIColor seg_setColorWithAny:anyColor dark:darkColor];
    } else if (anyColor) {
        return [UIColor seg_setColorWithAny:anyColor dark:anyColor];
    }
    NSString *hexStr = defaultColorDict[key];
    return [self colorWithHexString:hexStr];
}

#pragma mark - 主题色
+ (UIColor *)segColor_T {
    return [self segColor_TWithAlpha:1.0f];
}
+ (UIColor *)segColor_TWithAlpha:(CGFloat)alpha {
    UIColor *tintColor = [self readColorFromPlist:@"T"];
    return [tintColor colorWithAlphaComponent:alpha];
}
+(UIColor *)segColor_T_Press {
    return [self readColorFromPlist:@"TP"];
}

#pragma mark - 辅助色
+ (UIColor *)segColor_S1 {
    return [self readColorFromPlist:@"S1"];
}
+ (UIColor *)segColor_S1_Press {
    return [self readColorFromPlist:@"S1P"];
}
+ (UIColor *)segColor_S2 {
    return [self readColorFromPlist:@"S2"];
}
+ (UIColor *)segColor_S2_Press {
    return [self readColorFromPlist:@"S2P"];
}
+ (UIColor *)segColor_S3 {
    return [self readColorFromPlist:@"S3"];
}
+ (UIColor *)segColor_S3_Press {
    return [self readColorFromPlist:@"S3P"];
}
+ (UIColor *)segColor_S4 {
    return [self readColorFromPlist:@"S4"];
}
+ (UIColor *)segColor_S4_Press {
    return [self readColorFromPlist:@"S4P"];
}

#pragma mark - 字体颜色 (暗黑模式下字体透明度不同)
+ (UIColor *)segColor_1 {
    return [self readColorFromPlist:@"1"];
}
+ (UIColor *)segColor_2 {
    return [self readColorFromPlist:@"2"];
}
+ (UIColor *)segColor_3 {
    return [self readColorFromPlist:@"3"];
}
+ (UIColor *)segColor_C {
    return [self readColorFromPlist:@"C"];
}

#pragma mark - 灰色
+ (UIColor *)segColor_L2 {
    return [self readColorFromPlist:@"L2"];
}
+ (UIColor *)segColor_L3 {
    return [self readColorFromPlist:@"L3"];
}
+ (UIColor *)segColor_L4 {
    return [self readColorFromPlist:@"L4"];
}

#pragma mark - 背景颜色
+ (UIColor *)segColor_BG {
    return [self readColorFromPlist:@"BG"];
}
+ (UIColor *)segColor_BG3 {
    return [UIColor.blackColor colorWithAlphaComponent:0.5];
}

#pragma mark - 白色
+ (UIColor *)segColor_W {
    return [self segColor_W_alpha:1.0f];
}
+ (UIColor *)segColor_W_alpha:(CGFloat)alpha {
    return [[self readColorFromPlist:@"W"] colorWithAlphaComponent:alpha];
}
+ (UIColor *)segColor_W2 {
     return [self readColorFromPlist:@"W2"];
}
+ (UIColor *)segColor_W3 {
    return [self readColorFromPlist:@"W3"];
}
+ (UIColor *)segColor_TABLE {
    return [self readColorFromPlist:@"TABLE"];
}
 
#pragma mark - UIColor转换
+ (UIColor *)colorWithHexString:(NSString *)hexStr {
    CGFloat r, g, b, a;
    if (hexStrToRGBA(hexStr, &r, &g, &b, &a)) {
        return [UIColor colorWithRed:r green:g blue:b alpha:a];
    }
    return nil;
}
+ (NSString *)stringByTrim:(NSString *)string {
    NSCharacterSet *set = [NSCharacterSet whitespaceAndNewlineCharacterSet];
    return [string stringByTrimmingCharactersInSet:set];
}
static inline NSUInteger hexStrToInt(NSString *str) {
    uint32_t result = 0;
    sscanf([str UTF8String], "%X", &result);
    return result;
}
static BOOL hexStrToRGBA(NSString *str,
                         CGFloat *r, CGFloat *g, CGFloat *b, CGFloat *a) {
    str = [[SEGColorManager stringByTrim:str] uppercaseString];
    if ([str hasPrefix:@"#"]) {
        str = [str substringFromIndex:1];
    } else if ([str hasPrefix:@"0X"]) {
        str = [str substringFromIndex:2];
    }
    
    NSUInteger length = [str length];
    //         RGB            RGBA          RRGGBB        RRGGBBAA
    if (length != 3 && length != 4 && length != 6 && length != 8) {
        return NO;
    }
    
    //RGB,RGBA,RRGGBB,RRGGBBAA
    if (length < 5) {
        *r = hexStrToInt([str substringWithRange:NSMakeRange(0, 1)]) / 255.0f;
        *g = hexStrToInt([str substringWithRange:NSMakeRange(1, 1)]) / 255.0f;
        *b = hexStrToInt([str substringWithRange:NSMakeRange(2, 1)]) / 255.0f;
        if (length == 4)  *a = hexStrToInt([str substringWithRange:NSMakeRange(3, 1)]) / 255.0f;
        else *a = 1;
    } else {
        *r = hexStrToInt([str substringWithRange:NSMakeRange(0, 2)]) / 255.0f;
        *g = hexStrToInt([str substringWithRange:NSMakeRange(2, 2)]) / 255.0f;
        *b = hexStrToInt([str substringWithRange:NSMakeRange(4, 2)]) / 255.0f;
        if (length == 8) *a = hexStrToInt([str substringWithRange:NSMakeRange(6, 2)]) / 255.0f;
        else *a = 1;
    }
    return YES;
}


@end
